package com.duolaibei.business.view.goods.fragment;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.duolaibei.business.R;
import com.duolaibei.business.base.BaseFragment;
import com.duolaibei.business.utils.CommonUtil;
import com.duolaibei.business.utils.inject.Inject;
import com.duolaibei.business.view.goods.mode.TypesBean;
import com.duolaibei.business.view.goods.mode.TypesBeanManager;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created by Administrator on 2018/9/28.
 */

public class GoodTypesFragment extends BaseFragment {

    @Inject(R.id.recycle_types)
    private RecyclerView mRecycleTypes;

    private TypesAdapter mTypesAdapter;

    private List<TypesBean> mTypesList = new ArrayList<>();

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.fragment_types);
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        setTypesAdapter();
        initTypesData();
    }

    @Override
    protected void onSetListener() {
        mTypesAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                setTypeChoosed(position);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }

    private void setTypeChoosed(int position) {
        for (int i = 0; i < mTypesList.size(); i++) {
            if (position == i) {
                mTypesList.get(i).setIsChoosed(true);
            } else
                mTypesList.get(i).setIsChoosed(false);
        }
        mTypesAdapter.notifyDataSetChanged();
    }

    private void initTypesData() {
        List<TypesBean> list = TypesBeanManager.getInstance().selectAllTypes();
        mTypesList.clear();
        for (TypesBean typesBean : list) {
            typesBean.setIsChoosed(true);
            break;
        }
        mTypesList.addAll(list);
        mTypesAdapter.notifyDataSetChanged();
    }

    @Override
    public void onResume() {
        super.onResume();
        initTypesData();
    }

    private void setTypesAdapter() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecycleTypes.setLayoutManager(layoutManager);
        Drawable drawable = ContextCompat.getDrawable(Objects.requireNonNull(getActivity()), R.drawable.shape_recycle_div);
        DividerItemDecoration decoration = new DividerItemDecoration(Objects.requireNonNull(getActivity()), DividerItemDecoration.VERTICAL);
        if (drawable != null) {
            decoration.setDrawable(drawable);
            mRecycleTypes.addItemDecoration(decoration);
        } else
            mRecycleTypes.addItemDecoration(new DividerItemDecoration(Objects.requireNonNull(getActivity()), DividerItemDecoration.VERTICAL));
        mTypesAdapter = new TypesAdapter(getActivity(), R.layout.item_types_manage, mTypesList);
        mRecycleTypes.setAdapter(mTypesAdapter);
    }

    private class TypesAdapter extends CommonAdapter<TypesBean> {

        public TypesAdapter(Context context, int layoutId, List<TypesBean> datas) {
            super(context, layoutId, datas);
        }

        @Override
        protected void convert(ViewHolder holder, final TypesBean typesBean, int position) {
            holder.itemView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, CommonUtil.dip2px(60, getActivity())));
            /*RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.getView(R.id.type_name).getLayoutParams();
            params.setMarginStart(20);
            holder.getView(R.id.type_name).setLayoutParams(params);*/
            if (!typesBean.getIsChoosed()) {
                holder.itemView.setBackgroundColor(getResources().getColor(R.color.divider_line));
                holder.setTextColor(R.id.type_name, getResources().getColor(R.color.black));
            } else {
                holder.itemView.setBackgroundColor(getResources().getColor(R.color.white));
                holder.setTextColor(R.id.type_name, getResources().getColor(R.color.orangered));
            }
            holder.setText(R.id.type_name, typesBean.getName());
        }
    }
}
